package com.best.onegous.details;

import java.util.ArrayList;
import java.util.List;

import com.ab.adapter.AbImageShowAdapter;
import com.ab.util.AbStrUtil;
import com.ab.util.AbViewUtil;
import com.best.onegous.BaseActivity;
import com.best.onegous.R;

import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.ImageColumns;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Suggestions extends BaseActivity implements OnClickListener {
	private EditText feedbackEdit;
	private TextView txtFeedbackWordLeft;
	private GridView mGridView = null;
	private AbImageShowAdapter mImagePathAdapter = null;
	private ArrayList<String> mPhotoList = new ArrayList<String>();
	private int selectIndex = 0;
	private int camIndex = 0;
	private static final int PHOTO_PICKED_WITH_DATA = 3021;
	private static final int CAMERA_CROP_DATA = 3022;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setThem(R.layout.suggestions_activity);
		findViewById(R.id.back).setOnClickListener(this);

		feedbackEdit = (EditText) findViewById(R.id.feedbackEdit);
		txtFeedbackWordLeft = (TextView) findViewById(R.id.txtFeedbackWordLeft);

		final TextWatcher mTextEditorWatcher = new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				txtFeedbackWordLeft.setText(String.valueOf(140 - s.length()));
			}

			public void afterTextChanged(Editable s) {
			}
		};
		feedbackEdit.addTextChangedListener(mTextEditorWatcher);

		mGridView = (GridView) findViewById(R.id.myGrid);
		mPhotoList.add(String.valueOf(R.drawable.cam_photo));
		mImagePathAdapter = new AbImageShowAdapter(this, mPhotoList, 116, 116);
		mGridView.setAdapter(mImagePathAdapter);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectIndex = position;
				if (selectIndex == camIndex) {
					// �������ȥ��ȡ
					try {
						Intent intent = new Intent(Intent.ACTION_GET_CONTENT,
								null);
						intent.setType("image/*");
						startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
					} catch (ActivityNotFoundException e) {
						toast("打开相册失败！");
					}
				} else {
					/*
					 * for(int i=0;i<mImagePathAdapter.getCount();i++){
					 * AbImageShowAdapter.ViewHolder mViewHolder =
					 * (AbImageShowAdapter
					 * .ViewHolder)mGridView.getChildAt(i).getTag();
					 * if(mViewHolder!=null){
					 * mViewHolder.mImageView2.setBackgroundDrawable(null); } }
					 * 
					 * AbImageShowAdapter.ViewHolder mViewHolder =
					 * (AbImageShowAdapter.ViewHolder)view.getTag();
					 * mViewHolder.
					 * mImageView2.setBackgroundResource(R.drawable.photo_select
					 * );
					 */
				}
			}

		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		}
	}

	/**
	 * ��������Ϊ������Camera��Gally����Ҫ�ж����Ǹ��Եķ������,
	 * ��������ʱ�������startActivityForResult
	 */
	protected void onActivityResult(int requestCode, int resultCode,
			Intent mIntent) {
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case PHOTO_PICKED_WITH_DATA:
			Uri uri = mIntent.getData();
			String currentFilePath = getRealFilePath(this,uri);
			if (!AbStrUtil.isEmpty(currentFilePath)) {
				Intent intent1 = new Intent(this, CropImageActivity.class);
				intent1.putExtra("PATH", currentFilePath);
				startActivityForResult(intent1, CAMERA_CROP_DATA);
			} else {
				toast("ss"+uri);
			}
			break;

		case CAMERA_CROP_DATA:
			String path = mIntent.getStringExtra("PATH");
			Log.i("currentFilePathcurrentFilePathcurrentFilePath", path + "--");
			// if(D)Log.d(TAG, "�ü���õ���ͼƬ��·���� = " + path);
			mImagePathAdapter.addItem(mImagePathAdapter.getCount() - 1, path);
			camIndex++;
			AbViewUtil.setAbsListViewHeight(mGridView, 3, 3);
			break;
		}
	}


	
	public String getRealFilePath( final Context context, final Uri uri ) {
	    if ( null == uri ) return null;
	    final String scheme = uri.getScheme();
	    String data = null;
	    if ( scheme == null )
	        data = uri.getPath();
	    else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
	        data = uri.getPath();
	    } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
	        Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, null, null, null );
	        if ( null != cursor ) {
	            if ( cursor.moveToFirst() ) {
	                int index = cursor.getColumnIndex( ImageColumns.DATA );
	                if ( index > -1 ) {
	                    data = cursor.getString( index );
	                }
	            }
	            cursor.close();
	        }
	    }
	    return data;
	}
}
